package com.android.kuai.zhuan.utils;

import android.util.Log;

import java.util.Random;

public class Commands {

    public static void doTapBigOffset(int x, int y) {
        CommandExecution.execCommand("input tap " + getRandomPosition(x, 100) + " " + getRandomPosition(y, 100), true);
    }

    public static void doTapSmallOffset(int x, int y) {
        CommandExecution.execCommand("input tap " + getRandomPosition(x, 10) + " " + getRandomPosition(y, 10), true);
    }

    public static String doMoveLeft() {
        return getSwipeCommand(221, 627, 579, 627);

    }

    public static String doMoveRight() {
        return getSwipeCommand(579, 627, 221, 627);
    }

    public static String doMoveUp() {
        return getSwipeCommand(400, 400, 400, 800);
    }

    public static String doMovedown() {
        Log.d("leihuiming", "do move down");
        return getSwipeCommand(207, 992, 207, 546);
    }

    public static String pressHome() {
        return "input keyevent HOME";
    }

    public static void pressBack() {
        CommandExecution.execCommand("input keyevent BACK", true);
    }

    public static void pressBackTwice() {
        CommandExecution.execCommand("input keyevent BACK;input keyevent BACK", true);
    }

    /**
     * 指定范围内随机浮动
     * @param num  范围
     * @param offset  偏移
     * @return
     */
    public static int getRandomPosition(int num, int offset) {
        int min = num - offset;
        int max = num + offset;
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    /**
     * 范围内取随机值
     * @param max
     * @param min
     * @return
     */
    public static int getRandom(int max, int min) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

    private static String getTapCommand(int x, int y) {
        return "input tap " + getRandomPosition(x, 25) + " " + getRandomPosition(y, 25);
    }

    private static String getSwipeCommand(int fx, int fy, int tx, int ty) {
        int randomfx = getRandomPosition(fx, 61);
        int randomfy = getRandomPosition(fy, 52);
        int randomtx = getRandomPosition(tx, 54);
        int randomty = getRandomPosition(ty, 67);
        int duration = getRandomPosition(200, 50);
        return "input swipe " + randomfx + " " + randomfy + " " + randomtx + " " + randomty + " " + duration;
    }

    public static void stopApp(String pkgName) {
        CommandExecution.execCommand("am force-stop " + pkgName, true);
    }
}
